import asyncio
import re
from playwright.async_api import async_playwright, expect
import pandas as pd


# 获取所有的url
async def get_all_url(page, 直播间信息dict_room_info):    
    
    list_room_url = 直播间信息dict_room_info["直播间url"]

    #获取1~5的直播间url
    for i in range(1,21):
        # 翻页scroll，获取新url 
        await scroll_down(page,i) 
        # asyncio.sleep (1000)
        locator_new = page.locator("._mwZWwPK")              
        url_new = await locator_new.get_attribute("href")
        list_room_url.append(url_new)

    print(type(list_room_url) )
    for x in list_room_url:
        print(x)  
    return "1"  
  

# 滑动到指定元素
async def scroll_down(page, i):
    """
    Test verifies if the contact us section is visible and scrolls to it
    """    
    j = str(i) 
    element_name =  f'[data-rank="{j}"]' 
    print(element_name)
    print(type(element_name))


    # input("检查定位对象")
    base_locator = page.locator(element_name)        
    await expect(base_locator).to_be_visible()
    await base_locator.scroll_into_view_if_needed()




# 打开所有url网页
async def open_all_page(browser, 直播间信息dict_room_info):
    # 打开新页面
    page2 = await browser.new_page()

    list_room_url = 直播间信息dict_room_info["直播间url"]
    list_nickname = 直播间信息dict_room_info["播主名称name"]
    list_peoples = 直播间信息dict_room_info["观看人数peoples"]
    list_user_url = 直播间信息dict_room_info["播主个人url"]

    print(type(list_room_url),type(list_nickname),type(list_peoples),type(list_user_url))

    input("遍历房间url")
    for x in list_room_url:
        
        await page2.goto(x)         
        loc_peoples =  page2.locator(".UxWMHF9c")
        loc_nickname = page2.locator('[data-e2e="live-room-nickname"]')
        
        peoples = await loc_peoples.inner_text()
        nickname = await loc_nickname.inner_text()
        user_url = await loc_nickname.get_attribute("href")

        print(peoples + "\n",nickname + "\n", user_url + "\n")

        print(type(peoples),type(nickname),type(user_url))

        # input("等等")

    #   将采集到的数据,传入各自的列表
        list_nickname.append(nickname) 
        list_peoples.append(peoples)   
        list_user_url.append(user_url)  


       

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False, slow_mo=5000)
        page = await browser.new_page()
        start_url = "https://www.douyin.com/search/元梦之星?type=live"
        await page.goto(start_url)
        
        input("1.输入验证吗，enter打开直播间")

        
        # 构建直播间信息保存的词典dict
        list_room_url= []

        list_nickname = []
        list_peoples = []
        list_user_url=[]

        直播间信息dict_room_info = {"直播间url":list_room_url, "播主名称name":list_nickname, "观看人数peoples":list_peoples,"播主个人url": list_user_url}
  
        input("1.1 获取所有的url")
        await get_all_url(page, 直播间信息dict_room_info)

        input("1.2 获取所有的直播间信息")
        await open_all_page(browser, 直播间信息dict_room_info)
        
        sheet = pd.DataFrame(直播间信息dict_room_info)
        print(sheet)


        # UxWMHF9c

        input("2.enter关闭brwoser")
        # await browser.close()

asyncio.run(main())













